package fr.esisar.cs420.swt;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.widgets.Table;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;

import fr.esisar.cs420.traitement.Album;
import fr.esisar.cs420.traitement.Application;
import fr.esisar.cs420.traitement.Commande;
import fr.esisar.cs420.traitement.InfoAlbum;
import fr.esisar.cs420.traitement.Livre;
import fr.esisar.cs420.traitement.ParamCommandes;

public class GestionCommande extends Shell {
	private Text qte;
	private Table tableA;
	private int visible = 0;
	private String element = null ;
	private Text erreurQte;
	private Text erreurFormat;
	private Text erreurCommande;
	private Text erreurChoixAlbum;

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String args[]) {

	}

	/**
	 * Create the shell.
	 * @param display
	 * @throws SQLException 
	 */
	public GestionCommande(Display display) throws SQLException {
		super(display, SWT.SHELL_TRIM);
		
		Group grpVosAlbums = new Group(this, SWT.NONE);
		grpVosAlbums.setText("Vos albums/Livres");
		grpVosAlbums.setBounds(10, 10, 645, 192);
		
		final TableViewer tableAlbum = new TableViewer(grpVosAlbums, SWT.BORDER | SWT.FULL_SELECTION);
		tableA = tableAlbum.getTable();
		tableA.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				
				
				org.eclipse.swt.graphics.Point p = new org.eclipse.swt.graphics.Point(e.x,e.y);
				
				element = (String) tableAlbum.getCell(p).getElement() ;
				
			}
		});
		tableA.setBounds(10, 22, 281, 160);
		
		Label lblNewLabel = new Label(grpVosAlbums, SWT.NONE);
		lblNewLabel.setBounds(332, 25, 55, 15);
		lblNewLabel.setText("Quantité");
		
		qte = new Text(grpVosAlbums, SWT.BORDER);
		qte.setBounds(433, 22, 76, 21);
		
		Label lblListeDesFormats = new Label(grpVosAlbums, SWT.NONE);
		lblListeDesFormats.setBounds(329, 92, 98, 15);
		lblListeDesFormats.setText("Liste des formats");
		
		final CCombo listeFormat = new CCombo(grpVosAlbums, SWT.BORDER);
		listeFormat.setBounds(433, 86, 190, 21);
		listeFormat.setText("Choisissez un format");
		int i = 0;
		List<String> ArrayListeFormat = Commande.setFormat();
		while( i < ArrayListeFormat.size() ){
			listeFormat.add(ArrayListeFormat.get(i));
			i++;
		}
		
		erreurChoixAlbum = new Text(this, SWT.NONE);
		erreurChoixAlbum.setEditable(false);
		erreurChoixAlbum.setBounds(10, 200, 232, 21);
		
		
		Button newArticle = new Button(grpVosAlbums, SWT.NONE);
		newArticle.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				erreurQte.setText("");
				erreurFormat.setText("");
				erreurChoixAlbum.setText("");
				erreurCommande.setText("");
				if ( qte.getText().equals("") ){
					erreurQte.setText("Quantité non valide");
				}
				else if ( listeFormat.getText().equals("") || listeFormat.getText().equals("Choisissez un format") ){
					erreurFormat.setText("Format non choisi");
				}
				else if ( element == null ){
					erreurChoixAlbum.setText("Aucun album choisi");
				}
				
				else{
					ParamCommandes comClient;
					String titreAlbum ="";
					String prixFormat = "";
					int choixFormat = 0 ;
					try {
						titreAlbum = Commande.getTitre(element);
						prixFormat = Commande.getPrixFormat(listeFormat.getText());
						choixFormat = Commande.getIdFormat(listeFormat.getText());
						
					} catch (SQLException e1) {
						e1.printStackTrace();
					}
					comClient = new ParamCommandes(Album.getElementId(element),titreAlbum,Integer.valueOf(qte.getText()),choixFormat,listeFormat.getText(),prixFormat);
					Commande.listeCommandes.add(comClient);
					listeFormat.setText("Choisissez un format");
					qte.setText("");
				}
				
			}
		});
		newArticle.setBounds(433, 157, 137, 25);
		newArticle.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
			}
		});
		newArticle.setText("Nouvel article");
		
		erreurQte = new Text(grpVosAlbums, SWT.NONE);
		erreurQte.setEditable(false);
		erreurQte.setBounds(515, 22, 120, 21);
		
		erreurFormat = new Text(grpVosAlbums, SWT.NONE);
		erreurFormat.setEditable(false);
		erreurFormat.setBounds(433, 113, 177, 21);
		
		
		erreurCommande = new Text(this, SWT.NONE);
		erreurCommande.setEditable(false);
		erreurCommande.setBounds(227, 227, 232, 21);
		createContents();
		
		
		Button btnTerminerCommande = new Button(this, SWT.NONE);
		btnTerminerCommande.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				
				erreurCommande.setText("");
				if( Commande.listeCommandes.size() == 0 ){
					erreurCommande.setText("Vous n'avez effectué aucune commande");
				}
				else{
					Commande.saisieCommande(Application.client);
					for(int i=0;i<Commande.listeCommandes.size();i++)
						Commande.listeCommandes.remove(i);
					fermer();
					
				}
			}
		});
		btnTerminerCommande.setBounds(277, 254, 137, 25);
		btnTerminerCommande.setText("Terminer Commande");
		
		Button annuler = new Button(this, SWT.NONE);
		annuler.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				
				for(int i=0;i<Commande.listeCommandes.size();i++)
					Commande.listeCommandes.remove(i);
				fermer();
			}
		});
		annuler.setBounds(476, 254, 121, 25);
		annuler.setText("Annuler");
		
		

	
	    // The input will be an array or a collection.
	    // We will need every element of this input.
		tableAlbum.setContentProvider( new ArrayContentProvider());
		
	    // For very element to display, we will display its default string
		// (i.e. Object#toString()).
		tableAlbum.setLabelProvider( new LabelProvider());
			
		Album.getAlbums(visible);
		Livre.getLivre(visible);

		 // Eventually, let's define an input
		List<InfoAlbum> listAlbumPublic = Album.getList();
		
		 List<String> listAlbumString = new ArrayList<String>();
		 
		 i = 0 ;
		 
		 while( i < listAlbumPublic.size()) {
			 
			 listAlbumString.add(listAlbumPublic.get(i).getTitre());
			 i++;
		 }
		
		
		tableAlbum.setInput( listAlbumString );


	}

	protected void fermer() {
		// TODO Auto-generated method stub
		this.dispose();
	}

	/**
	 * Create contents of the shell.
	 */
	protected void createContents() {
		setText("Saisie Commande");
		setSize(679, 345);

	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}
